<template>
    <h1>用户购买理财页面</h1>

    <table>
        <tr>
            <td>理财产品名称</td>
            <td>{{ info.F_Name }}</td>
        </tr>
        <tr>
            <td>年收益</td>
            <td>{{ info.F_APY }}</td>
        </tr>
        <tr>
            <td>最低金额</td>
            <td>{{ info.F_MinMoney }}</td>
        </tr>
        <tr>
            <td>风险等级</td>
            <td>{{ info.F_Level==0?'低风险':info.F_Level==1?'中风险':'高风险' }}</td>
       
        </tr>
        <tr>
            <td>购买金额</td>
            <td><input type="text" v-model="Fmoney" @change="ChangeApy()"/></td>
        </tr>
        <tr>
            <td>每年预计收益</td>
            <td>{{FapyMoney}}CNY</td>
        </tr>
        <tr>
            <td><input type="button" value="保存" @click="SaveData"/></td>
         
        </tr>
    </table>


</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useRoute } from 'vue-router';
import {useStore} from '../../store/index.ts'
import { useRouter } from 'vue-router';

const store = useStore();

const router = useRouter();

const route = useRoute();

const info = ref({
    F_Name:"",
    F_APY:"",
    F_MinMoney:"",
    F_Level:0
})

const Fmoney = ref(0);
const FapyMoney = ref(0);

onMounted(() => {
    LoadDate();
})

//反填
const LoadDate =() =>{
    axios({
        method: 'get',
        url:'http://localhost:53279/api/Financing/EditFinancing',
        params:{
            Fid:route.params.F_Id
        }
    })
    .then(function (res) {
        info.value = res.data;
    })
    .then(function (error) {
        console.log(error);
    })
}

////计算预计收益
const ChangeApy=()=>{
    axios.get('http://localhost:53279/api/Financing/GetMoney',{
        params:{
            UFmoney:Fmoney.value,
            Fapy:info.value.F_APY,         
        }
        
    })
    .then((res)=>{        
        console.log(res.data);
        FapyMoney.value=res.data;
       
    })
    .catch((err)=>{
        console.log(err);
    })
}
//alert(route.params.F_Id);

//添加用户理财信息
const SaveData = () =>{
//alert(store.UserId);

    axios({
        method: 'post',
        url:'http://localhost:53279/api/Financing/AddUserFinancing',
        data:{
            U_Id:store.UserId,
            F_Id:route.params.F_Id,
            UF_Money:Fmoney.value
        }
    })
    .then(function (res) {
        if(res.data>0){
            alert("保存成功");
            router.push('/UserFinancingShow');
        }
        else if(res.data==-3){
            alert("用户理财金额小于最低金额");
            return;
        }
        else{
            alert("保存失败");
            return;
        }
    })
    .then(function (error) {
        console.log(error);
    })
}


</script>
